package com.taotao.manage.controller.api;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.taotao.manage.pojo.Item;
import com.taotao.manage.pojo.ItemDesc;
import com.taotao.manage.service.ItemDescService;
import com.taotao.manage.service.ItemService;

@Controller
@RequestMapping("api/item")
public class ApiItemController {
	@Autowired
	private ItemService itemService;
	@Autowired
	private ItemDescService itemDescService;
	//private static final ObjectMapper mapper = new ObjectMapper();        
	/**
     * 根据ID查询商品信息
     * @param itemId
     * @return
     */
    @RequestMapping(value = "{itemId}", method = RequestMethod.GET)
    public ResponseEntity<Item> queryItemById(@PathVariable("itemId") Long itemId) {
        try {
            Item item = this.itemService.queryById(itemId);
            if (item != null) {
                return ResponseEntity.ok(item);
            }
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
    
    @RequestMapping(value = "desc/{itemId}", method = RequestMethod.GET)
    public ResponseEntity<ItemDesc> queryByItemDescId(@PathVariable("itemId") Long itemId) {
    	try {
    		ItemDesc itemDesc = this.itemDescService.queryById(itemId);
    		if (itemDesc != null) {
    			return ResponseEntity.ok(itemDesc);
    		}
    		return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    	} catch (Exception e) {
    		e.printStackTrace();
    		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    	}
    }

	 /**
     * 查询全部商品类目信息，并封装为tree结构
     * @return
     */
//    @RequestMapping(method = RequestMethod.GET)
//    public ResponseEntity<String> queryItemCat(@RequestParam(value="callback", required = false)String callback) {
//        try {
//            // 调用service获取结果
//            ItemCatResult result = this.itemCatService.queryAllToTree();
//            
//            // 将结果转为JSON
//            String jsonResult = mapper.writeValueAsString(result);
//            // 判断是否是跨域请求,如果是，则添加跨域回调函数
//            if(StringUtils.isNotEmpty(callback)){
//                return ResponseEntity.ok(callback + "("+jsonResult+")");
//            }
//            // 如果不是，直接返回结果和成功状态码
//            return ResponseEntity.ok(jsonResult);
//        } catch (Exception e) {
//            e.printStackTrace();
//            // 返回异常状态码
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
//        }
//    }
}

